package cn.edu.svtcc.util;

import cn.edu.svtcc.domain.Grade;
import cn.edu.svtcc.domain.Student;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;
import java.util.stream.Collectors;

public class PropertiesSaver {
    private static final Path YAML_PATH = Paths.get("src/main/resources/application.yml");
    // 修改原始 saveStudents 方法为 saveAllData
    public static synchronized void saveAllData(
            List<Student> students,
            List<Grade> grades
    ) {
        try {
            // 1. 读取原始 YAML 文件
            Map<String, Object> yamlData = new LinkedHashMap<>();
            if (Files.exists(YAML_PATH)) {
                Yaml yamlReader = new Yaml();
                String existingYaml = new String(Files.readAllBytes(YAML_PATH), StandardCharsets.UTF_8);
                Object loaded = yamlReader.load(existingYaml);
                if (loaded instanceof Map) {
                    yamlData.putAll((Map<String, Object>) loaded);
                }
            }

            // 2. 构建 students.list
            List<Map<String, Object>> studentList = students.stream()
                    .map(s -> {
                        Map<String, Object> map = new LinkedHashMap<>();
                        map.put("number", s.getNumber());
                        map.put("name", s.getName());
                        map.put("age", s.getAge());
                        map.put("className", s.getClassName());
                        return map;
                    })
                    .collect(Collectors.toList());
            Map<String, Object> studentsWrapper = new LinkedHashMap<>();
            studentsWrapper.put("list", studentList);
            yamlData.put("students", studentsWrapper);

            // 3. 构建 grades.list
            List<Map<String, Object>> gradeList = new ArrayList<>();
            for (Grade g : grades) {
                Map<String, Object> gradeMap = new LinkedHashMap<>();
                gradeMap.put("gradeID", g.getGradeID());
                gradeMap.put("studentNumber", g.getStudentNumber());
                gradeMap.put("course", g.getCourse());
                gradeMap.put("score", g.getScore());
                gradeList.add(gradeMap);
            }
            Map<String, Object> gradesWrapper = new LinkedHashMap<>();
            gradesWrapper.put("grades", gradeList);
            yamlData.put("students", studentsWrapper);
            yamlData.put("grades", gradesWrapper);
            // 4. 写入文件
            DumperOptions options = new DumperOptions();
            options.setIndent(2);
            options.setPrettyFlow(true);
            Yaml yamlWriter = new Yaml(options);
            try (Writer writer = Files.newBufferedWriter(YAML_PATH, StandardCharsets.UTF_8,
                    StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
                yamlWriter.dump(yamlData, writer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
